package com.e3.basdoc.goods.service.impl;

import java.util.Date;
import java.util.List;

import com.e3.basdoc.goods.dao.GoodsDAO;
import com.e3.basdoc.goods.entity.GoodsEntity;
import com.e3.basdoc.goods.model.GoodsModel;
import com.e3.basdoc.goods.service.GoodsCacheManager;
import com.e3.basdoc.goods.service.GoodsService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperService;

public class GoodsServiceImpl extends SuperService implements GoodsService{
	
	private GoodsDAO goodsDAO;
	
	private GoodsDAO getGoodsDAO(){
		if(goodsDAO == null){
			goodsDAO = DAOHelper.createDAOInstance(GoodsDAO.class);
		}
		return goodsDAO;
	}

	public GoodsModel newGoods(GoodsModel model) throws ServiceException {
		validateGoods(model);
		checkGoodsExisted(model);
		
		GoodsEntity entity = goodsModelToEntity(model, null);
		getGoodsDAO().persist(entity);
		
		model.setId(entity.getId());
		
		GoodsCacheManager.INSTANCE.put(model);
		
		return model;
	}

	public GoodsModel modifyGoods(GoodsModel model) throws ServiceException {
		validateGoods(model);

		long id = model.getId();
		GoodsEntity entity = getGoodsDAO().getById(GoodsEntity.class, id);
		if (entity == null) {
			return newGoods(model);
		}

		String oldCode = entity.getCode();
		if (!oldCode.equals(model.getCode())) {
			checkGoodsExisted(model);
		}

		goodsModelToEntity(model, entity);
		getGoodsDAO().merge(entity);
		
		GoodsCacheManager.INSTANCE.put(model);
		return model;
	}

	public void deleteGoodsByIds(List<Long> ids) throws ServiceException {
		if(ids == null||ids.isEmpty()){
			return;
		}
		
		getGoodsDAO().deleteGoodsByIds(ids);
		
		GoodsCacheManager.INSTANCE.remove(ids);
	}

	public void checkGoodsExisted(GoodsModel model) throws ServiceException {
		if (model == null) {
			return;
		}

		String code = model.getCode();
		String name = model.getName();
		
		StringBuffer msgs = new StringBuffer();
		long size = getGoodsDAO().getGoodsSizeByCode(code);
		if(size>0){
			msgs.append("编码为'" + code+"' ");
		}
		size = getGoodsDAO().getGoodsSizeByName(name);
		if (size>0) {
			msgs.append("名称为'" + name + "' ");
		}
		if(msgs.length()>0){
			msgs.append("已经存在，请设置新的编码和名称。");
			throw new ServiceException(msgs.toString());
		}
	}

	private void validateGoods(GoodsModel model) throws ServiceException{
		String code = model.getCode();
		String name = model.getName();

		if (code == null || code.trim().length() == 0 
			|| name == null || name.trim().length() == 0
			) {
			throw new ServiceException("请设置编码,名称和符号。");
		}
	}
	
	private GoodsEntity goodsModelToEntity(GoodsModel model,GoodsEntity entity){
		entity = ocModelToEntity(GoodsEntity.class, model, entity);

		entity.setCode(model.getCode());
		entity.setName(model.getName());
		entity.setShortName(model.getShortName());
		entity.setAllowNegative(model.isAllowNegative());
		entity.setAllowReturn(model.isAllowReturn());
		entity.setBarcode(model.getBarcode());
		entity.setBrand(model.getBrand());
		entity.setBulk(model.getBulk());
		entity.setCeilingPrice(model.getCeilingPrice());
		entity.setExportRebatesRate(model.getExportRebatesRate());
		entity.setFloorPrice(model.getFloorPrice());
		entity.setForeignLangName(model.getForeignLangName());
		entity.setHasInvoice(model.isHasInvoice());
		entity.setOrigin(model.getOrigin());
		entity.setPurchaseLossRate(model.getPurchaseLossRate());
		entity.setReserveLossRate(model.getReserveLossRate());
		entity.setRefCost(model.getRefCost());
		entity.setRefPrice(model.getRefPrice());
		entity.setRemark(model.getRemark());
		entity.setSealFlag(model.isSealFlag());
		entity.setSealPersonId(model.getSealPersonId());
		
		Date date = model.getSealTime();
		if(date!=null){
			entity.setSealTime(date.getTime());
		}
		
		entity.setTaxId(model.getTaxId());
		entity.setType(model.getType());
		entity.setMainSupplierId(model.getMainSupplierId());
		entity.setMeasureUnitId(model.getMeasureUnitId());
		entity.setSpecification(model.getSpecification());
		entity.setWeight(model.getWeight());
		
		return entity;

	}
}
